package edu.zut.cs.network.Experiment5;

import org.apache.log4j.*;
import java.util.Scanner;

public class AddMatrix {
    private static Logger logger = Logger.getLogger(AddMatrix.class);
    public static int[][] addMatrix(int[][] m1, int[][] m2){
        int[][] sum = new int[m1.length][m1[0].length];
        for(int i = 0; i < m1.length; i++){
            for(int j = 0; j < m1[0].length;j++){
                sum[i][j] = m1[i][j] + m2[i][j];
            }
        }
        return sum;
    }

    public static int[][] init(int row, int col){
        int[][] arr = new int [row][col];
        logger.info("请输入此二维数组的元素：");
        Scanner scanner = new Scanner(System.in);
        for(int i = 0; i < row; i++) {
            String str1 = scanner.nextLine();
            String[] str2 = str1.split("[ ]+");
            for(int j = 0; j < col; j++){
                arr[i][j] = Integer.parseInt(str2[j]);
            }
        }
        return arr;
    }

    public static void printMatrix(int[][] ar1, int[][] ar2, int[][] sum){
        if((ar1.length & 1) == 1){
//            行数为奇数
            for(int i = 0; i < ar1.length; i++) {
                if(i == ((ar1.length-1)/2)){
//                    行数是中间的那一行
                    for(int j = 0; j < ar1[0].length;j++){
                        System.out.print((ar1[i][j])+"  ");
                    }
                    System.out.print("  +    ");
                    for(int j = 0; j < ar2[0].length;j++){
                        System.out.print((ar2[i][j])+"  ");
                    }
                    System.out.print("  =    ");
                    for(int j = 0; j < sum[0].length;j++){
                        System.out.print((sum[i][j])+"  ");
                    }
                    System.out.println();
                }
                else{
                    for(int j = 0; j < ar1[0].length;j++) {
                        System.out.print((ar1[i][j])+"  ");
                    }
                    System.out.print("       ");
                    for(int j = 0; j < ar2[0].length;j++){
                        System.out.print((ar2[i][j])+"  ");
                    }
                    System.out.print("       ");
                    for(int j = 0; j < sum[0].length;j++){
                        System.out.print((sum[i][j])+"  ");
                    }
                    System.out.println();
                    }
                }
            }
            else{
//                行数为偶数
            for(int i = 0; i < ar1.length; i++) {
                if(i == ((ar1.length)/2)) {
//                    行数是(ar1.length)/2
                    for (int j = 0; j < ar1[0].length; j++) {
                        System.out.print((ar1[i][j])+"  ");
                    }
                    System.out.print("  +    ");
                    for (int j = 0; j < ar2[0].length; j++) {
                        System.out.print((ar2[i][j])+"  ");
                    }
                    System.out.print("  =    ");
                    for (int j = 0; j < sum[0].length; j++) {
                        System.out.print((sum[i][j])+"  ");
                    }
                    System.out.println();
                }
                else{
                    for(int j = 0; j < ar1[0].length;j++) {
                        System.out.print((ar1[i][j])+"  ");
                    }
                    System.out.print("       ");
                    for(int j = 0; j < ar2[0].length;j++){
                        System.out.print((ar2[i][j])+"  ");
                    }
                    System.out.print("       ");
                    for(int j = 0; j < sum[0].length;j++){
                        System.out.print((sum[i][j])+"  ");
                    }
                    System.out.println();
                    }
                }
            }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        logger.info("请输入二维数组的行数：");
        int row = input.nextInt();
        logger.info("请输入二维数组的列数：");
        int col = input.nextInt();
        logger.info("您输入的二维数组是"+row+"行"+col+"列的");
        int[][] arr1 = AddMatrix.init(row,col);
        int[][] arr2 = AddMatrix.init(row,col);
        int[][] sum = AddMatrix.addMatrix(arr1,arr2);
        AddMatrix.printMatrix(arr1,arr2,sum);
    }
}